/**
 * 
 */
package com.huniu.analysis.colony;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;

import org.dom4j.Element;

import com.huniu.analysis.model.HtmlQuality;
import com.huniu.analysis.model.QualityXMLProperty;
import com.huniu.utility.constant.GlobalConstants;

/**
 * @author zandy (chenheng)
 *
 */
public class LeaderBeeAnalyseImpl extends AbstractBeeAnalyseImpl implements
		ILeaderBeeAnalyse {
	private Element htmlQuality ;

	public void setHtmlQuality(Element htmlQuality) {
		this.htmlQuality = htmlQuality;
	}
	
	@SuppressWarnings("unchecked")
	public void analyse() {
		String nectar = htmlQuality.attributeValue(QualityXMLProperty.ELEMENT_NECTAR);
		String value = htmlQuality.attributeValue(QualityXMLProperty.ELEMENT_VALUE);
		String link = htmlQuality.getText();
		HtmlQuality quality = new HtmlQuality();
		quality.setDictionary(nectar);
		quality.setQuality(Integer.parseInt(value));
		quality.setLink(link);
		CacheManager singletonManager = CacheManager.create();
		Cache cache = singletonManager.getCache(GlobalConstants.HTML_QUALITY_CACHE_NAME);
		net.sf.ehcache.Element pair = cache.get(nectar);
		List<HtmlQuality> htmlQualityList = null ;
		if ( pair == null ){
			htmlQualityList = new ArrayList<HtmlQuality>();
			htmlQualityList.add(quality);
		} else {
			htmlQualityList = (List<HtmlQuality>) pair.getValue();
			htmlQualityList.add(quality);
		}
		Collections.sort(htmlQualityList);
		pair = new net.sf.ehcache.Element(nectar,htmlQualityList);
		cache.put(pair);
	}
}
